<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import { computed, ref } from 'vue';
	import type { ILocationBreed } from '@/simba/types/plant.d';
	import { useLocationBreed } from '@/simba/hooks/locationBreed'
	const { rules, getLocationBreed, addLocationBreed, deleteLocationBreed } = useLocationBreed() 

	import sheep from '@/sheep';
	const user = computed(() => sheep.$store('user').userInfo);
	
	const formData = ref<ILocationBreed>({
		id: null,
		name: '',
		type: '',
		sortNo: 0,
		userId: null
	})
		
	onLoad( async (options?:AnyObject)=> {
		//根据参数设置页面标题
		uni.setNavigationBarTitle({
			title: options.type.concat("", options.id === undefined ? '新增' : '修改')
		})
		if(options.id !== undefined){
			//根据获取位置、品种信息（修改）
			formData.value = await getLocationBreed(options.id)
		}
		else{
			formData.value.type=options.type
			formData.value.userId= user.value.id
		}
	})
	
</script>

<template>
	<up-form
		label-position="left"
		ref="locBreedFormRef"
		v-model="formData"
		:rules="rules"
		:model="formData"
		>
		 <!-- validateTrigger="bind"
		  labelWidth="160"
		  labelAlign="left"
		  border
		  :labelStyle="{ fontWeight: 'bold' }" -->
		<view class="">
			<up-form-item 
				name="name" 
				label="名称" 
				:borderBottom="true"
				class="form-item">
					<up-input
						v-model="formData.name"
						border="bottom"
						placeholder="请填写名称"
						clearable
					></up-input>
			  </up-form-item>
		</view>
	</up-form>

	<view class="ss-m-b-20">
	  <up-button type="primary" text="保存" @tap="addLocationBreed(formData)"></up-button>
	</view>
	<up-button v-if="formData.id" type="error" :plain="true" :hairline="true" @tap="deleteLocationBreed(formData)">删除</up-button>
</template>

<style lang="scss">

</style>
